<template>
  <Modal
    v-model="form.visible"
    :width="1000"
    :mask-closable="false"
    :loading="loading"
    :title="'确认兑换商品'"
    ok-text="提交"
    @on-ok="ok">
    <Row class="mt30">
      <Col span="16" offset="4">
        <Form ref="form" :model="form.data" :rules="ruleValidate" :label-width="110">
          <FormItem label="确认时间" required>
            <Row>
              <Col span="24">
                <Row>
                  <Col span="11">
                    <FormItem prop="confirmDt">
                      <DatePicker type="datetime" style="width:100%" placeholder="选择时间" v-model="form.data.confirmDt"></DatePicker>
                    </FormItem>
                  </Col>
                </Row>
              </Col>
              <Col span="12"></Col>
            </Row>
          </FormItem>
          <FormItem prop="confirmNote" label="兑换商品说明">
            <Input type="textarea" v-model="form.data.confirmNote" placeholder="请输入" :rows="4"></Input>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </Modal>
</template>

<script>
  import { mapGetters } from 'vuex'
  import vipApi from '@/api/vip'

  export default {
    name: 'update-score-modal',
    props: [
      'form'
    ],
    components: {
    },
    data () {
      return {
        loading: true,
        ruleValidate: {
          confirmDt: [
             { required: true, type: 'date', message: '请选择确认时间', trigger: 'change' }
          ],
          confirmNote: [
            { max: 100, message: '兑换说明字符长度应小于100', trigger: 'change' }
          ]
        }
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'userInfo'
      })
    },
    watch: {
      'form.visible': 'onClose'
    },
    methods: {
      ok () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.submitForm()
          } else {
            this.$Message.error('表单验证失败，请检查输入项目!')
            this.form.visible = true
            this.loading = false
            setTimeout(() => {
              this.loading = true
            }, 100)
          }
        })
      },
      onClose (isVisible) {
        if (!isVisible) {
          this.$refs.form.resetFields()
        }
      },
      async submitForm () {
        this.form.data.pkRcId = this.userInfo.pkRclubInfo
        if (this.form.data.id) {
          const { body } = await vipApi.createComfigScoreUsed(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('修改成功')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
            }, 500)
          }
        } else {
          const { body } = await vipApi.createComfigScoreUsed(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('修改成功')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
            }, 500)
          }
        }
        this.form.visible = false
        this.$emit('submit')
      }
    }
  }
</script>
